package pt.inescid.components.policyengine.heimdall.pap.base;

import pt.inescid.components.policyengine.heimdall.base.EventObj;
import pt.inescid.components.policyengine.heimdall.pdp.base.ModelInstance;
import pt.inescid.components.policyengine.heimdall.pdp.base.RuleInstance;

public class ReevaluationDefinition {

	private RuleDefinition start;
	private String every;
	private RuleDefinition stop;
	private String action;
	
	private RuleInstance auxStartRuleInstance;
	private RuleInstance auxStopRuleInstance;

	

	
	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      CONSTRUCTORS       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public ReevaluationDefinition(String action) {
		super();
		this.action = action;
	}


	/* ===========================================================
	==============================================================
	=============                    =============================
	=============      startsReevaluation       =============================
	=============                    =============================
	==============================================================
	=========================================================== */
	public boolean startsReevaluation(EventObj ce) {
		
		ModelInstance currentModel = new ModelInstance(ce);
		if (auxStartRuleInstance == null) {
			if (start == null) {
				return (true);
			}
			else {
				auxStartRuleInstance = RuleInstance.getInstance(
						start, currentModel, null);
			}
		}
		
		if (auxStartRuleInstance != null) {
			return (((Boolean) auxStartRuleInstance.evaluate(currentModel,
					false)).
					booleanValue());
		}
		else {
			return (true);
		}
	} // startsReevaluation
		

	
	
	
	public String getAction() {
		return action;
	}


	public String getEvery() {
		return every;
	}


	public RuleDefinition getStart() {
		return start;
	}


	public RuleDefinition getStop() {
		return stop;
	}


	public void setAction(String action) {
		this.action = action;
	}


	public void setEvery(String every) {
		this.every = every;
	}


	public void setStart(RuleDefinition start, 
			RuleDefinition previousLevelExp) {
		
		if (previousLevelExp == null) {
			this.start = start;
		}
		else {
			((CompositeDefinition) previousLevelExp).addOperand(start);
		}
	}

	public void setStop(RuleDefinition stop, 
			RuleDefinition previousLevelExp) {
		
		if (previousLevelExp == null) {
			this.stop = stop;
		}
		else {
			((CompositeDefinition) previousLevelExp).addOperand(stop);
		}
	}
	
	

	
}; // ReevaluationDefinition
